<?php

// +----------------------------------------------------------------------
// | Copyright (c) 2020 All rights reserved.
// +----------------------------------------------------------------------
// | User: Simony <1362229026@qq.com>
// | wechat: Shen86579595
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\controller\Common;
use think\Db;

class Task extends Common
{
    /**
     * User:Simony
     * Function: 任务列表列表
     * @return mixed
     */
    public function taskList()
    {
        //params
        $field = input('get.field', '');
        $value = input('get.value', '');
        $search_field = input('get.search_field', '');
        $search_value = input('get.search_value', '');

        //where
        $where = [];

        //judge
        if ($field) $where[$field] = $value;
        if ($search_field) $where[$search_field] = $search_value;

        //model
        $taskModel = model('common/task/Task');

        //getData
        $list = $taskModel->getList($where, 10);


        //assign
        $this->assign('weekarray', ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]);
        $this->assign('list', $list);
        $this->assign('page', $taskModel->page_info->render());
        $this->assign('types', $taskModel->types);
        return $this->fetch();
    }


    /**
     * User:Simony
     * Function: 任务列表添加修改
     */
    public function taskAdd()
    {
        //params
        $id = input('param.id/d', '');

        //model
        $taskModel = model('common/task/Task');
        $data = $taskModel->getInfoByID($id);

        if ($this->request->isAjax()) {
            //post params
            $ajax_data = $this->request->post();
            $cycle = $ajax_data['type'] == 0 ? strtotime($ajax_data['cycle_' . $ajax_data['type']]) : $ajax_data['cycle_' . $ajax_data['type']];

            $data = [
                'name' => $ajax_data['name'],
                'class_name' => $ajax_data['class_name'],
                'function_name' => $ajax_data['function_name'],
                'type' => $ajax_data['type'],
                'cycle' => $cycle,
                'begin_time' => strtotime($ajax_data['begin_time']),
                'end_time' => strtotime($ajax_data['end_time']),
                'state' => $ajax_data['state'],
            ];

            //judge
            if ($id && !$data) ds_json_encode('err', '数据不存在');

            $res = $id ? $taskModel->edit(['id' => $id], $data) : $taskModel->add($data);
            $res ? ds_json_encode('ret', '操作成功') : ds_json_encode('err', '操作失败');
        }

        $cycle = '';
        $data && $data['type']!=0 && $cycle=$data['cycle'];

        $this->assign('weekarray', ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]);
        $this->assign('cycle', $cycle);
        $this->assign('data', $data);
        $this->assign('types', $taskModel->types);
        return $this->fetch();
    }

}